package sqlite_conf_table

import (
	"database/sql"
	"github.com/sirupsen/logrus"
	"go-iot-gateway/internal/conf_table/define"
	"strings"
)

/*
id
chl_id
chl_addr
chl_name
chl_desc
chl_protocol
chl_type
chl_colt_freq
chl_wait_time
protocol_type
chl_enable_bkup
reserve0
reserve1
chl_main_param
chl_bkup_param
chl_valid
*/

var tbChannelInfoColumns = []string{
	"id",
	"chl_id",
	"chl_addr",
	"chl_name",
	"chl_desc",
	"chl_protocol",
	"chl_type",
	"chl_colt_freq",
	"chl_wait_time",
	"protocol_type",
	"chl_enable_bkup",
	"reserve0",
	"reserve1",
	"chl_main_param",
	"chl_bkup_param",
	"chl_valid",
}

type tbChannelInfoEntity struct {
	id              *int
	chl_id          *string
	chl_addr        *int64
	chl_name        *string
	chl_desc        *string
	chl_protocol    *string
	chl_type        *int
	chl_colt_freq   *int
	chl_wait_time   *int
	protocol_type   *int
	chl_enable_bkup *int
	reserve0        *string
	reserve1        *string
	chl_main_param  *string
	chl_bkup_param  *string
	chl_valid       *int
}

var (
	//tbChannelInfoEntityPtr unsafe.Pointer
	_channelConfInfoArr map[string]*conf_table_define.ChannelInfo //key chl_id
)

func ChannelInfoList() map[string]*conf_table_define.ChannelInfo {
	return _channelConfInfoArr
	//return *(*[]conf_table.ChannelInfo)(atomic.LoadPointer(&tbChannelInfoEntityPtr))
}

func selectChannelInfo(db *sql.DB) error {
	//查询 channel info
	fields := strings.Join(tbChannelInfoColumns, ",")
	selectSql := "select " + fields + " from tb_channel_info order by id"
	rows, err := db.Query(selectSql)
	if err != nil {
		return err
	}
	defer func() {
		err := rows.Close()
		if err != nil {
			logrus.Warn("行关闭失败")
			return
		}
	}()

	_channelConfInfoArr, err = convertChannelInfo(rows)
	if err != nil {
		return err
	}
	//atomic.StorePointer(&deviceConfInfoEntitySptr, unsafe.Pointer(&_data))
	return nil
}

func convertChannelInfo(rows *sql.Rows) (map[string]*conf_table_define.ChannelInfo, error) {
	err := rows.Err()
	if err != nil {
		return nil, err
	}

	resultMap := make(map[string]*conf_table_define.ChannelInfo)
	for rows.Next() {
		entity := tbChannelInfoEntity{}
		err := rows.Scan(
			&entity.id,
			&entity.chl_id,
			&entity.chl_addr,
			&entity.chl_name,
			&entity.chl_desc,
			&entity.chl_protocol,
			&entity.chl_type,
			&entity.chl_colt_freq,
			&entity.chl_wait_time,
			&entity.protocol_type,
			&entity.chl_enable_bkup,
			&entity.reserve0,
			&entity.reserve1,
			&entity.chl_main_param,
			&entity.chl_bkup_param,
			&entity.chl_valid)

		if err != nil {
			return nil, err
		}
		//todo 默认值
		item := &conf_table_define.ChannelInfo{}

		if entity.id != nil {
			item.Id = *entity.id
		}
		if entity.chl_id != nil {
			item.Chl_id = strings.TrimSpace(*entity.chl_id)
		}
		if entity.chl_addr != nil {
			item.Chl_addr = *entity.chl_addr
		}
		if entity.chl_name != nil {
			item.Chl_name = strings.TrimSpace(*entity.chl_name)
		}
		if entity.chl_desc != nil {
			item.Chl_desc = strings.TrimSpace(*entity.chl_desc)
		}
		if entity.chl_protocol != nil {
			item.Chl_protocol = *entity.chl_protocol
		}
		if entity.chl_type != nil {
			item.Chl_type = *entity.chl_type
		}
		if entity.chl_colt_freq != nil {
			item.Chl_colt_freq = *entity.chl_colt_freq
		}
		if entity.chl_wait_time != nil {
			item.Chl_wait_time = *entity.chl_wait_time
		}
		if entity.protocol_type != nil {
			item.Protocol_type = *entity.protocol_type
		}
		if entity.reserve0 != nil {
			item.Reserve0 = *entity.reserve0
		}
		if entity.reserve1 != nil {
			item.Reserve1 = *entity.reserve1
		}
		if entity.chl_main_param != nil {
			item.Chl_main_param = *entity.chl_main_param
		}
		if entity.chl_bkup_param != nil {
			item.Chl_bkup_param = *entity.chl_bkup_param
		}
		if entity.chl_valid != nil {
			item.Chl_valid = *entity.chl_valid
		}
		resultMap[item.Chl_id] = item
	}

	return resultMap, nil
}
